local mushroom_grow_nodes = {"default:dirt", "default:dirt_with_grass", "default:dirt_with_rainforest_litter", "default:dirt_with_coniferous_litter"}

local poisoned = {}

minetest.register_node(":flowers:mushroom_red", { --Amanita Muscaria (Fly Agaric)
	description = "Red Mushroom",
	tiles = {"flowers_mushroom_red.png"},
	inventory_image = "flowers_mushroom_red.png",
	drawtype = "plantlike",
	paramtype = "light",
	sunlight_propagates = true,
	walkable = false,
	buildable_to = true,
	groups = {snappy = 3, attached_node = 1, flammable = 1, mushroom = 1, toxic_mushroom = 1},
	sounds = default.node_sound_leaves_defaults(),
	on_use = minetest.item_eat(-5),
	selection_box = {
		type = "fixed",
		fixed = {-4 / 16, -0.5, -4 / 16, 4 / 16, -1 / 16, 4 / 16},
	}
})


minetest.register_node("mushrooms_redo:mushroom_white", { --Amanita Smithiana (Smith's Amanita)
	description = "White Mushroom",
	tiles = {"mushroom_white.png"},
	inventory_image = "mushroom_white.png",
	drawtype = "plantlike",
	paramtype = "light",
	sunlight_propagates = true,
	walkable = false,
	buildable_to = true,
	groups = {snappy = 3, attached_node = 1, flammable = 1, mushroom = 1, toxic_mushroom = 3},
	sounds = default.node_sound_leaves_defaults(),
	on_use = function(itemstack, player)
		if not poisoned[player:get_player_name()] then
			minetest.after(10,
			function(player)
				if player and player:is_player() then
					player:set_hp(0)
				end
			end, player)
			poisoned[player:get_player_name()] = true
		end
		return minetest.item_eat(5)(itemstack, player)
	end,
	selection_box = {
		type = "fixed",
		fixed = {-3 / 16, -0.5, -3 / 16, 3 / 16, 1 / 16, 3 / 16},
	}
})

minetest.register_on_dieplayer(function(player)
	poisoned[player:get_player_name()] = nil
end)
minetest.register_on_leaveplayer(function(player)
	if poisoned[player:get_player_name()] then 
		player:set_hp(0)
	end
	poisoned[player:get_player_name()] = nil
end)

minetest.register_node(":flowers:mushroom_brown", { --Lentinula Edodes (Shiitake)
	description = "Brown Mushroom",
	tiles = {"flowers_mushroom_brown.png"},
	inventory_image = "flowers_mushroom_brown.png",
	drawtype = "plantlike",
	paramtype = "light",
	sunlight_propagates = true,
	walkable = false,
	buildable_to = true,
	groups = {snappy = 3, attached_node = 1, flammable = 1, mushroom = 1},
	sounds = default.node_sound_leaves_defaults(),
	on_use = minetest.item_eat(1),
	selection_box = {
		type = "fixed",
		fixed = {-3 / 16, -0.5, -3 / 16, 3 / 16, -2 / 16, 3 / 16},
	}
})

minetest.register_node("mushrooms_redo:mushroom_brown_cooked", {
	description = "Cooked Brown Mushroom",
	tiles = {"flowers_mushroom_brown.png^[colorize:#501F08:200"},
	inventory_image = "flowers_mushroom_brown.png^[colorize:#501F08:200",
	drawtype = "plantlike",
	paramtype = "light",
	sunlight_propagates = true,
	walkable = false,
	buildable_to = true,
	groups = {snappy = 3, attached_node = 1, flammable = 1},
	sounds = default.node_sound_leaves_defaults(),
	on_use = minetest.item_eat(6),
	selection_box = {
		type = "fixed",
		fixed = {-3 / 16, -0.5, -3 / 16, 3 / 16, -2 / 16, 3 / 16},
	}
})
minetest.register_craft({
    type = "cooking",
    output = "mushrooms_redo:mushroom_brown_cooked",
    recipe = "flowers:mushroom_brown",
    cooktime = 8,
})


minetest.register_craftitem("mushrooms_redo:sinister_bread", {
	description = "Bread",
	inventory_image = "farming_bread.png",
	on_use = function(itemstack, player)
		if not poisoned[player:get_player_name()] then
			minetest.after(10,
			function(player)
				if player and player:is_player() then
					player:set_hp(0)
				end
			end, player)
			poisoned[player:get_player_name()] = true
		end
		return minetest.item_eat(5)(itemstack, player)
	end,
	groups = {flammable = 2},
})

minetest.register_craft({
	output = "mushrooms_redo:sinister_bread",
	type="shapeless",
	recipe={"farming:bread","mushrooms_redo:mushroom_white"},
})

--[[
Nullify default mushroom spread. group:mushroom now handles this. 
Delete the brackets on the next line to fall back to default (mushrooms registered by use of group:mushroom will no longer grow). 
]]


flowers.mushroom_spread = function() end 

minetest.register_abm({
	nodenames={"group:mushroom"},
	interval=15,
	chance=24,
	catch_up = false,
	action=function(p, node)
		local shroom = node.name
		local light = minetest.get_node_light(p, 0.5)
		if not light then return end
		local node_reg = minetest.registered_nodes[shroom]
		if not node_reg then return end
		p.x = p.x + 2*(math.random() - 0.5)
		p.y = p.y + 2*(math.random() - 0.5)
		p.z = p.z + 2*(math.random() - 0.5)
		if minetest.get_node(p).name ~= "air" then return end
		local soil_reg = minetest.registered_nodes[minetest.get_node(vector.new(p.x,p.y-1,p.z)).name]
		if 3 <= light and light <= 5 and soil_reg and soil_reg.groups.soil then
			minetest.set_node(p, node)
		end
	end
})--]]

local mg_name = minetest.get_mapgen_setting("mg_name")
if mg_name == "v6" then
	minetest.register_decoration({
		deco_type = "simple",
		place_on = mushroom_grow_nodes,
		sidelen = 16,
		noise_params = {
			offset = 0,
			scale = 0.04,
			spread = {x = 100, y = 100, z = 100},
			seed = 7133,
			octaves = 3,
			persist = 0.6
		},
		biomes = {"rainforest", "jungle", "deciduous_forest", "coniferous_forest", "floatland_coniferous_forest"},
		y_min = 1,
		y_max = 30,
		decoration = "mushrooms_redo:mushroom_white",
		spawn_by = "group:tree",
		num_spawn_by = 1,
	})
else
	minetest.register_decoration({
		deco_type = "simple",
		place_on = mushroom_grow_nodes,
		sidelen = 16,
		noise_params = {
			offset = 0,
			scale = 0.006,
			spread = {x = 250, y = 250, z = 250},
			seed = 2,
			octaves = 3,
			persist = 0.66
		},
		biomes = {"rainforest", "jungle", "deciduous_forest", "coniferous_forest", "floatland_coniferous_forest"},
		y_min = 1,
		y_max = 31000,
		decoration = "mushrooms_redo:mushroom_white",
	})
end